/*****************************************************
*                                                                          
*               COPYRIGHT (c) 2017-2019 Hargic Corporation                 
*                         All Rights Reserved                              
*                                                                          
* The source code contained or described herein and all documents          
* related to the source code ("Material") are owned by Hargic            
* Corporation or its licensors.  Title to the Material remains             
* with Hargic Corporation or its suppliers and licensors.                  
*                                                                          
* The Material is protected by worldwide copyright and trade secret        
* laws and treaty provisions. No part of the Material may be used,         
* copied, reproduced, modified, published, uploaded, posted, transmitted,  
* distributed, or disclosed in any way except in accordance with the       
* applicable license agreement.                                            
*                                                                          
* No license under any patent, copyright, trade secret or other            
* intellectual property right is granted to or conferred upon you by       
* disclosure or delivery of the Materials, either expressly, by            
* implication, inducement, estoppel, except in accordance with the         
* applicable license agreement.                                            
*                                                                          
* Unless otherwise agreed by Hargic in writing, you may not remove or      
* alter this notice or any other notice embedded in Materials by Hargic    
* or Hargic's suppliers or licensors in any way.                           
*                                                                          
** Author: shengda.huang
** Date: 2019-02-21
*
*****************************************************/

#ifndef __KV_H_
#define __KV_H_

#ifdef __cplusplus
extern "C"{
#endif

/***** INCLUDES *************************************/
#include "common.h"
#include "dymem.h"

/***** DEFINES **************************************/

enum
{
    KV_OK = 0,
    KV_E_NO_SPACE,
    KV_E_NOT_FIND,
    KV_E_K_EXCEEDS,
    KV_E_V_EXCEEDS,
};

#define KV_KEY_NAME_LEN_MAX         128

/***** TYPEDEFS *************************************/
struct kv_list
{
    struct kv_list *next;
    struct kv_list *prev;
    u16 k_len;
    u16 v_len;
    u8 data[0];
};

typedef struct 
{
    struct kv_list *top;
    dymem_obj_t pool;
    u8 *heap;
    u32 heap_size;
} kv_obj_t;

/***** CONSTANTS ************************************/

/***** VARIABLES ************************************/

/***** EXTERNAL VARIABLES ***************************/

/***** EXTERNAL FUNCTIONS ***************************/

/***** PROTOTYPES ***********************************/
void kv_init(kv_obj_t *_this);
i32 kv_set(kv_obj_t *_this, const char *key, void *value, u32 size);
i32 kv_get(kv_obj_t *_this, const char *key, void *value, u32 maxsize);
i32 kv_del(kv_obj_t *_this, const char *key);


#ifdef __cplusplus
}
#endif

#endif /* __KV_H_ */

